import axios from "axios"
import { ossSign } from "~/api/constant/common";

export function transDate(str = '') {
    str = str.split('-');
    return str[1] + '月' + str[2].split(' ')[0] + '日';
}

export function upLoadImage(file) {
    return new Promise((resolve, reject) => {
        ossSign()
            .then(result => {
                let key =
                    new Date().toLocaleDateString() +
                    "/" +
                    new Date().getTime() +
                    ".png";
                var formData = new FormData();
                formData.append("key", key);
                formData.append("policy", result.policy);
                formData.append("OSSAccessKeyId", result.accessid);
                formData.append("success_action_status", "200");
                formData.append("signature", result.signature);
                formData.append("file", file);
                formData.append("host", result.host);
                axios({
                    method: "POST",
                    url: result.host,
                    data: formData,
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                })
                    .then(_data => {
                        var imgUrl = result.host + "/" + formData.get("key");
                        resolve(imgUrl)
                    })
                    .catch(err => {
                        reject(err)
                    });
            })
            .catch(err => {
                reject(err)
            });
    })
}